package cn.hp.servlet;

import cn.hp.service.UserService;
import cn.hp.service.UserServiceImp;
import cn.hp.util.PageBean;
import cn.hp.util.ResultJson;
import cn.hp.util.ReturnJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 * ajax ，并以json格式反会结果
 * 除了findById方法之外
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService service= new UserServiceImp();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp){
        String method = req.getParameter("method");
        if("pageFind".equals(method)){
            pageFind(req,resp);
        }else if("find".equals(method)){
            find(req,resp);
        }else if("save".equals(method)){
            save(req,resp);
        }else if("upp".equals(method)){
            upp(req,resp);
        }else if("del".equals(method)){
            del(req,resp);
        }else if("findById".equals(method)){
            findById(req,resp);
        }else{
            System.out.println("未知访问："+method);
        }
    }
    private void pageFind(HttpServletRequest req, HttpServletResponse resp) {
        int limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        int page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        String name = req.getParameter("name")!=null?"%"+req.getParameter("name")+"%":"%%";
        String phone = req.getParameter("phone")!=null?" and phone =" +req.getParameter("name"):"";
        String sql = "select * from user where uname like ? "+phone;
        PageBean<Map> list = service.pageFind(page,limit,sql,name);
        ReturnJsonData.returnJson(ResultJson.success(list),resp);
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp){
        Integer id = req.getParameter("id")!=null?Integer.valueOf(req.getParameter("id")):0;
        String sql = "select * from user where uid=? ";
        Map map = service.findById(sql,id);
        ReturnJsonData.returnJson(map!=null?ResultJson.success(map):ResultJson.error(),resp);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp){
        Integer id = req.getParameter("id")!=null?Integer.valueOf(req.getParameter("id")):0;
        String sql = "delete from user where uid = ?";
        int i = service.del(sql,id);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp){
        String uusername = req.getParameter("uusername");
        String upassword = req.getParameter("upassword");
        String uname = req.getParameter("uname");

        String sql = "insert into user (uusername,upassword,uname) values(?,?,?)";
        int i = service.save(sql,uusername,upassword,uname);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    private void upp(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("uid");
        String uname = req.getParameter("uname");
        String sql = "update user set uname = ? where uid=?";
        int i = service.upp(sql,uname,id);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    /**
     * 查询
     */
    private void find(HttpServletRequest req, HttpServletResponse resp){
        List<Map> list = service.find("select * from user");
        ReturnJsonData.returnJson(ResultJson.success(list),resp);
    }
}
